/*
 *    Copyright 2019 CManLH
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.lifeonwalden.fa4py.common;

/**
 * 字典项
 */
public class DicItem {
    private char[][] ctxAlphabet;
    private char[] alphabet;

    public DicItem(char[][] alphabet) {
        this.ctxAlphabet = alphabet;

        if (this.ctxAlphabet.length > 1) {
            boolean[] tmp = new boolean[26];
            int size = 0;
            for (char[] ctx : alphabet) {
                if (null != ctx) {
                    for (char c : ctx) {
                        if (c - 97 < 0) {
                            System.out.println(c);
                        }
                        if (!tmp[c - 97]) {
                            tmp[c - 97] = true;
                            size++;
                        }
                    }
                }
            }
            this.alphabet = new char[size];
            int idx = 0;
            for (int i = 0; i < tmp.length; i++) {
                if (tmp[i]) {
                    this.alphabet[idx++] = (char) (i + 97);
                }
            }
        }
    }

    public char[] getAlphabet(int context) {
        if (context < ctxAlphabet.length && context >= 0 && null != ctxAlphabet[context]) {
            return ctxAlphabet[context];
        } else if (-1 == context) {
            if (null != alphabet) {
                return alphabet;
            } else {
                return ctxAlphabet[0];
            }
        } else {
            return ctxAlphabet[0];
        }
    }
}
